//
//  MonthView.m
//  carcareIOS
//
//  Created by ileo on 16/6/27.
//  Copyright © 2016年 chezheng. All rights reserved.
//

#import "MonthView.h"
#import "CalendarMgr.h"
#import "HorizontalTableView.h"
#import "WeekDaysView.h"
#import "DrivesDayView.h"
#import "UIScrollView+LoadMore.h"
#import "DrivesModel.h"
#import "MonthTitleView.h"
#import "MonthBottomView.h"

#define MonthHeight 45
#define WeekTitleHeight 25
#define DayHeight (__SCREEN_IS_3_5 ? 42 : 50)
#define DayHeightMax (DayHeight * 6)
#define GAP 22

#define CellTag 1

@interface MonthView () <HorizontalTableViewDataSourse,WeekDaysViewDataSource,LoadMoreControllerDelegate,MonthTitleViewDelegate,MonthBottomViewDelegate>

@property (nonatomic, strong) UIView *weekTitleView;//星期标题

@property (nonatomic, strong) MonthTitleView *monthTitleView;
@property (nonatomic, strong) MonthBottomView *monthBottomView;

@property (nonatomic, strong) HorizontalTableView *horWeekTableView;
@property (nonatomic, strong) HorizontalTableView *horMonthTableView;

@property (nonatomic, copy) NSArray *horMonthFirstDays;//横向滑动月第一天的数组
@property (nonatomic, copy) NSArray *horWeekFirstDays;//横向滑动周第一天的数组

@property (nonatomic, assign) CGFloat currentWeekTop;
@property (nonatomic, assign) CGFloat lastHeight;

@property (nonatomic, assign) BOOL isHeightChanging;//需要设置是否正在变动高度

@property (nonatomic, strong) NSDate *lastestDate;
@property (nonatomic, strong) NSDate *nextestDate;

@end

@implementation MonthView

-(void)dealloc{
    [self removeObserver:self forKeyPath:@"frame"];
}

-(instancetype)initWithTop:(CGFloat)top choiceDate:(NSDate *)date nextestDate:(NSDate *)nextestDate lastestDate:(NSDate *)lastestDate{
    self = [super initWithFrame:CGRectMake(0, top, __MAIN_WIDTH, 0)];
    if (self) {
        self.lastestDate = lastestDate;
        self.nextestDate = nextestDate;
        
        self.backgroundColor = COLOR_HEXSTRING(@"#F6F6F9");
        
        self.clipsToBounds = YES;
        self.choiceDate = date;
        
        [self addSubview:self.horMonthTableView];
        [self addSubview:self.horWeekTableView];
        [self addSubview:self.monthTitleView];
        [self addSubview:self.weekTitleView];
        [self addSubview:self.monthBottomView];
        self.monthTitleView.top = 0;
        self.weekTitleView.top = self.monthTitleView.bottom;
        self.horWeekTableView.top = self.weekTitleView.bottom;
        
        [self addObserver:self forKeyPath:@"frame" options:(NSKeyValueObservingOptionOld|NSKeyValueObservingOptionNew) context:nil];
        self.height = [MonthView heightMin];
    }
    return self;
}

-(void)updateFrame{
    CGFloat height = self.height;
    self.horMonthTableView.bottom = height - GAP;
    self.monthBottomView.bottom = height;
    self.horWeekTableView.hidden = (height >= ([MonthView heightMax] - self.currentWeekTop));
}

-(void)reloadData{
    [self.horWeekTableView reloadData];
    [self.horMonthTableView reloadData];
}

+(CGFloat)heightMax{
    return MonthHeight + WeekTitleHeight + DayHeightMax + GAP;
}

+(CGFloat)heightMin{
    return MonthHeight + WeekTitleHeight + DayHeight + GAP;
}

-(void)updateMonthTitle{
    
    if (self.horWeekTableView.hidden) {
        NSArray *cells = self.horMonthTableView.visibleCells;
        if (cells.count > 0) {
            HorizontalTableViewCell *cell = cells[0];
            NSInteger index = cell.row;
            if (self.horMonthFirstDays.count - 1 < index) {
                index = self.horMonthFirstDays.count - 1;
            }
            NSDate *date = self.horMonthFirstDays[index];
            if (self.delegate) {
                [self.delegate monthDidShowMonth:[CalendarMgr componentsWithDate:date]];
            }
            NSDateComponents *com = [CalendarMgr componentsWithDate:date];
            NSString *title = [NSString stringWithFormat:@"%zd年%zd月",com.year,com.month];
            BOOL canLast = [self canAddLastMonthFirstDayWithDate:[CalendarMgr firstDayInLastMonthForDate:date]];
            BOOL canNest = [self canAddNextMonthFirstDayWithDate:[CalendarMgr firstDayInNextMonthForDate:date]];
            [self.monthTitleView updateTitle:title canLoadLast:canLast canLoadNext:canNest];
        }
    }else{
        NSArray *cells = self.horWeekTableView.visibleCells;
        if (cells.count > 0) {
            HorizontalTableViewCell *cell = cells[0];
            NSInteger index = cell.row;
            NSDate *week = self.horWeekFirstDays[index];
            NSDate *date = [CalendarMgr firstDayInMonthForDate:week];
            if (self.delegate) {
                [self.delegate monthDidShowMonth:[CalendarMgr componentsWithDate:date]];
            }
            NSDateComponents *com = [CalendarMgr componentsWithDate:date];
            NSString *title = [NSString stringWithFormat:@"%zd年%zd月",com.year,com.month];
            
            BOOL canLast = [self canAddLastWeekFirstDayWithDate:[CalendarMgr firstDayInLastWeekForDate:week]];
            BOOL canNest = [self canAddNextWeekFirstDayWithDate:[CalendarMgr firstDayInNextWeekForDate:week]];
            
            [self.monthTitleView updateTitle:title canLoadLast:canLast canLoadNext:canNest];
        }
    }

}

#pragma mark - set get

-(void)setIsHeightChanging:(BOOL)isHeightChanging{

    _isHeightChanging = isHeightChanging;
    if (self.horMonthTableView.visibleCells > 0 && self.horWeekTableView.visibleCells > 0) {
        if (isHeightChanging) {
            if (self.height < self.lastHeight) {
                NSInteger row = ((HorizontalTableViewCell *)self.horMonthTableView.visibleCells[0]).row;
                NSDate *firstDay = self.horMonthFirstDays[row];
                self.horMonthFirstDays = @[firstDay];
                NSArray *arr = [self sundaysInMonthWithFirstDay:firstDay];
                CGFloat height = DayHeightMax / arr.count;
                __block BOOL hasWeek = NO;
                __weak __typeof(self) wself = self;
                NSDate *lastest = [CalendarMgr firstDayInWeekForDate:self.lastestDate];
                __block NSInteger lastestIndex = 0;
                NSDate *choice = [CalendarMgr firstDayInWeekForDate:self.choiceDate];
                __block NSInteger choiceIndex = -1;
                [arr enumerateObjectsUsingBlock:^(NSDate *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    if ([CalendarMgr isSameDayWithDate1:obj date2:wself.horWeekFirstDays[0]]) {
                        hasWeek = YES;
                        wself.currentWeekTop = idx * height;
                        *stop = YES;
                    }
                    if ([CalendarMgr isSameDayWithDate1:obj date2:lastest]) {
                        lastestIndex = idx;
                    }
                    if ([CalendarMgr isSameDayWithDate1:obj date2:choice]) {
                        choiceIndex = idx;
                    }
                }];
                if (!hasWeek) {
                    NSInteger index = (choiceIndex >= 0) ? choiceIndex : lastestIndex;
                    self.horWeekFirstDays = @[arr[index]];
                    self.currentWeekTop = index * height;
                }
            }else{
                NSInteger row = ((HorizontalTableViewCell *)self.horWeekTableView.visibleCells[0]).row;
                NSDate *firstDay = self.horWeekFirstDays[row];
                self.horWeekFirstDays = @[firstDay];
                NSDate *firstMonthDay = [CalendarMgr firstDayInMonthForDate:firstDay];
                self.horMonthFirstDays = @[firstMonthDay];
                NSArray *arr = [self sundaysInMonthWithFirstDay:firstMonthDay];
                CGFloat height = DayHeightMax / arr.count;
                __weak __typeof(self) wself = self;
                [arr enumerateObjectsUsingBlock:^(NSDate *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    if ([CalendarMgr isSameDayWithDate1:obj date2:wself.horWeekFirstDays[0]]) {
                        wself.currentWeekTop = idx * height;
                        *stop = YES;
                    }
                }];
            }
            [self reloadData];
        }
    }
    
}

-(void)setChoiceDate:(NSDate *)choiceDate{
    _choiceDate = choiceDate;
    self.horWeekFirstDays = @[[CalendarMgr firstDayInWeekForDate:choiceDate]];
    self.horMonthFirstDays = @[[CalendarMgr firstDayInMonthForDate:choiceDate]];
    [self reloadData];
}

-(UIView *)weekTitleView{
    if (!_weekTitleView){
        _weekTitleView = [UIView viewWithFrame:CGRectMake(0, 0, self.width, WeekTitleHeight)];
        _weekTitleView.backgroundColor = [UIColor whiteColor];
        CGFloat width = self.width / 7;
        NSArray *titles = @[@"周日",@"周一",@"周二",@"周三",@"周四",@"周五",@"周六"];
        [titles enumerateObjectsUsingBlock:^(NSString *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [[[UILabel labelWithFrame:CGRectMake(idx * width, 0, width - 1, WeekTitleHeight) font:FONT_DEFAULT_Light(11) text:obj textColor:COLOR_HEXSTRING(@"#909090")] labelResetTextAlignment:NSTextAlignmentCenter] setupOnView:_weekTitleView];
        }];
    }
    return _weekTitleView;
}

-(MonthTitleView *)monthTitleView{
    if (!_monthTitleView) {
        _monthTitleView = [[MonthTitleView alloc] initWithFrame:CGRectMake(0, 0, self.width, MonthHeight)];
        _monthTitleView.delegate = self;
    }
    return _monthTitleView;
}

-(MonthBottomView *)monthBottomView{
    if (!_monthBottomView) {
        _monthBottomView = [[MonthBottomView alloc] initWithFrame:CGRectMake(0, self.height - GAP, __MAIN_WIDTH, GAP)];
        _monthBottomView.delegate = self;
    }
    return _monthBottomView;
}



-(HorizontalTableView *)horWeekTableView{
    if (!_horWeekTableView) {
        _horWeekTableView = [[HorizontalTableView alloc] initWithFrame:CGRectMake(0, 0, self.width, DayHeight)];
        _horWeekTableView.dataSource = self;
        _horWeekTableView.pagingEnabled = YES;
        _horWeekTableView.showsHorizontalScrollIndicator = NO;
        [_horWeekTableView setLoadMoreDelegate:self];
        [_horWeekTableView setLoadMoreLeftView:[[LoadMoreView alloc] initWithFrame:CGRectMake(0, 0, 1, _horWeekTableView.height)]];
        [_horWeekTableView setLoadMoreRightView:[[LoadMoreView alloc] initWithFrame:CGRectMake(0, 0, 1, _horWeekTableView.height)]];
        _horWeekTableView.backgroundColor = [UIColor whiteColor];
    }
    return _horWeekTableView;
}

-(HorizontalTableView *)horMonthTableView{
    if (!_horMonthTableView) {
        _horMonthTableView = [[HorizontalTableView alloc] initWithFrame:CGRectMake(0, 0, self.width, DayHeightMax)];
        _horMonthTableView.dataSource = self;
        _horMonthTableView.showsHorizontalScrollIndicator = NO;
        _horMonthTableView.pagingEnabled = YES;
        [_horMonthTableView setLoadMoreDelegate:self];
        [_horMonthTableView setLoadMoreLeftView:[[LoadMoreView alloc] initWithFrame:CGRectMake(0, 0, 1, _horWeekTableView.height)]];
        [_horMonthTableView setLoadMoreRightView:[[LoadMoreView alloc] initWithFrame:CGRectMake(0, 0, 1, _horWeekTableView.height)]];
        _horMonthTableView.backgroundColor = [UIColor whiteColor];
    }
    return _horMonthTableView;
}

-(WeekDaysView *)weekDaysView{
    WeekDaysView *weekDaysView = [[WeekDaysView alloc] initWithFrame:CGRectMake(0, 0, self.width, DayHeight)];
    weekDaysView.dataSource = self;
    return weekDaysView;
}

-(CGFloat)monthHeightWithDate:(NSDate *)date{
    return DayHeight * [CalendarMgr weeksCountInMonthForDate:date];
}

-(NSArray<NSDate *> *)sundaysInMonthWithFirstDay:(NSDate *)date{
    NSMutableArray *tmp = [NSMutableArray arrayWithCapacity:6];
    NSDate *firSunday = [CalendarMgr firstDayInWeekForDate:date];
    NSInteger weeks = [CalendarMgr weeksCountInMonthForDate:date];
    [tmp addObject:firSunday];
    for (NSInteger i = 1; i < weeks; i++) {
        [tmp addObject:[CalendarMgr dateFromDate:firSunday afterDays:7 * i]];
    }
    return [tmp copy];
}

-(NSArray<NSDateComponents *> *)weekDaysFromSunday:(NSDate *)date{
    NSMutableArray *tmp = [NSMutableArray arrayWithCapacity:6];
    [tmp addObject:[CalendarMgr componentsWithDate:date]];
    for (NSInteger i = 1; i < 7; i++) {
        [tmp addObject:[CalendarMgr componentsWithDate:[CalendarMgr dateFromDate:date afterDays:i]]];
    }
    return [tmp copy];
}

#pragma mark - loadmore
-(void)loadMoreLeftFinish:(void (^)(CGFloat))finish withScrollView:(UIScrollView *)scrollView{
    BOOL succ;
    if (scrollView == self.horWeekTableView) {
        succ = [self addLastWeekFirstDay];
        [self.horWeekTableView reloadData];
    }else{
        succ = [self addLastMonthFirstDay];
        [self.horMonthTableView reloadData];
    }
    finish(succ?scrollView.width:0);
}

-(void)loadMoreRightFinish:(void (^)())finish withScrollView:(UIScrollView *)scrollView{
    if (scrollView == self.horWeekTableView) {
        [self addNextWeekFirstDay];
        [self.horWeekTableView reloadData];
    }else{
        [self addNextMonthFirstDay];
        [self.horMonthTableView reloadData];
    }
    finish();
}

-(BOOL)addLastWeekFirstDay{
    NSDate *last = [CalendarMgr firstDayInLastWeekForDate:[self.horWeekFirstDays firstObject]];
    if ([self canAddLastWeekFirstDayWithDate:last]) {
        NSMutableArray *arr = [NSMutableArray arrayWithArray:self.horWeekFirstDays];
        [arr insertObject:last atIndex:0];
        self.horWeekFirstDays = [arr copy];
        return YES;
    }
    return NO;
}

-(BOOL)addNextWeekFirstDay{
    NSDate *next = [CalendarMgr firstDayInNextWeekForDate:[self.horWeekFirstDays lastObject]];
    if ([self canAddNextWeekFirstDayWithDate:next]) {
        NSMutableArray *arr = [NSMutableArray arrayWithArray:self.horWeekFirstDays];
        [arr addObject:next];
        self.horWeekFirstDays = [arr copy];
        return YES;
    }
    return NO;
}

-(BOOL)addLastMonthFirstDay{
    NSDate *last = [CalendarMgr firstDayInLastMonthForDate:[self.horMonthFirstDays firstObject]];
    if ([self canAddLastMonthFirstDayWithDate:last]) {
        NSMutableArray *arr = [NSMutableArray arrayWithArray:self.horMonthFirstDays];
        [arr insertObject:last atIndex:0];
        self.horMonthFirstDays = [arr copy];
        return YES;
    }
    return NO;
}

-(BOOL)addNextMonthFirstDay{
    NSDate *next = [CalendarMgr firstDayInNextMonthForDate:[self.horMonthFirstDays lastObject]];
    if ([self canAddNextMonthFirstDayWithDate:next]) {
        NSMutableArray *arr = [NSMutableArray arrayWithArray:self.horMonthFirstDays];
        [arr addObject:next];
        self.horMonthFirstDays = [arr copy];
        return YES;
    }
    return NO;
}

-(BOOL)canAddLastWeekFirstDayWithDate:(NSDate *)date{
    NSDate *lastest = [CalendarMgr dateFromDate:[CalendarMgr firstDayInWeekForDate:self.lastestDate] beforeDays:1];
    return [CalendarMgr isDate1:date laterThanDate2:lastest];
}

-(BOOL)canAddNextWeekFirstDayWithDate:(NSDate *)date{
    return [CalendarMgr isDate1:self.nextestDate laterThanDate2:date];
}

-(BOOL)canAddLastMonthFirstDayWithDate:(NSDate *)date{
    NSDate *lastest = [CalendarMgr dateFromDate:[CalendarMgr firstDayInMonthForDate:self.lastestDate] beforeDays:1];
    return [CalendarMgr isDate1:date laterThanDate2:lastest];
}

-(BOOL)canAddNextMonthFirstDayWithDate:(NSDate *)date{
    return [CalendarMgr isDate1:self.nextestDate laterThanDate2:date];
}

-(BOOL)canLoadLastDay{
    return [CalendarMgr isDate1:self.choiceDate laterThanDate2:self.lastestDate];
}

-(BOOL)canLoadNextDay{
    return [CalendarMgr isDate1:self.nextestDate laterThanDate2:self.choiceDate];
}

#pragma mark - hortableview datasource
-(NSInteger)numberOfRowsWithHorizontalTableView:(HorizontalTableView *)horizontalTableView{
    return horizontalTableView == self.horMonthTableView ? self.horMonthFirstDays.count : self.horWeekFirstDays.count;
}
-(HorizontalTableViewCell *)horizontalTableView:(HorizontalTableView *)horizontalTableView cellForRow:(NSInteger)row{
    NSString *cellID = @"cellID";
    HorizontalTableViewCell *cell = [horizontalTableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[HorizontalTableViewCell alloc] initWithReuseIdentifier:cellID];
        cell.backgroundColor = [UIColor whiteColor];
    }
    [cell removeAllSubviews];
    if (self.horMonthTableView == horizontalTableView) {
        NSDate *date = self.horMonthFirstDays[row];
        NSArray *firstDays = [self sundaysInMonthWithFirstDay:date];
        CGFloat height = DayHeightMax / firstDays.count;
        __weak __typeof(self) wself = self;
        [firstDays enumerateObjectsUsingBlock:^(NSDate *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            WeekDaysView *weekDaysView = [wself weekDaysView];
            weekDaysView.top = idx * height;
            [cell addSubview:weekDaysView];
            [weekDaysView setDateComponents:[wself weekDaysFromSunday:obj] monthFirstDay:date isShowOneMonth:YES];
        }];
    }else{
        NSDate *date = self.horWeekFirstDays[row];
        WeekDaysView *weekDaysView = [self weekDaysView];
        [cell addSubview:weekDaysView];
        [weekDaysView setDateComponents:[self weekDaysFromSunday:date] monthFirstDay:[CalendarMgr firstDayInMonthForDate:date] isShowOneMonth:NO];
    }
    return cell;
}
-(CGFloat)horizontalTableView:(HorizontalTableView *)horizontalTableView widthForRow:(NSInteger)row{
    return __MAIN_WIDTH;
}

-(void)horizontalTableView:(HorizontalTableView *)horizontalTableView displayCells:(NSArray *)cells{
    if (cells.count == 1) {
        [self updateMonthTitle];
    }
}

#pragma mark - weekdays datasource

-(UIView<DayView> *)dayViewWithDateComponents:(NSDateComponents *)dateComponents monthFirstDay:(NSDate *)monthFirstDay isShowOneMonth:(BOOL)isShowMonth{
    DrivesDayView *dayView = [[DrivesDayView alloc] init];
    DrivesDayModel *model = [[DrivesDayModel alloc] init];
    NSDate *date = [[NSCalendar currentCalendar] dateFromComponents:dateComponents];
    model.isThisMonth = [CalendarMgr isSameMonthWithDate1:date date2:monthFirstDay];
    model.isToday = [CalendarMgr isTodayWithDate:date];
    model.isChoiced = [CalendarMgr isSameDayWithDate1:date date2:self.choiceDate];
    model.isNotData = !([CalendarMgr isDate1:date laterThanDate2:self.lastestDate] && [CalendarMgr isDate1:self.nextestDate laterThanDate2:date]);
    model.isShowWeek = !isShowMonth;
    if (self.delegate) {
        model.isDrived = [self.delegate monthHadDrivesOnDate:dateComponents];
    }
    __weak __typeof(self) wself = self;
    dayView.Click = ^(NSDateComponents *date){
        if (wself.delegate) {
            [wself.delegate monthDidClickOnDate:dateComponents];
        }
    };
    
    [dayView setupDataComponents:dateComponents drivesDayModel:model];
    
    return dayView;
}

#pragma mark - monthtitleview delegate
-(void)monthTitleViewClickOnLast{
    if (self.horMonthTableView.visibleCells.count == 0) {
        return;
    }
    if (self.horWeekTableView.hidden) {
        NSInteger index = ((HorizontalTableViewCell *)(self.horMonthTableView.visibleCells[0])).row;
        if (index > 0) {
            [self.horMonthTableView displayRow:index - 1 animation:YES];
        }else{
            NSMutableArray *tmp = [NSMutableArray arrayWithArray:self.horMonthFirstDays];
            [tmp insertObject:[CalendarMgr firstDayInLastMonthForDate:[self.horMonthFirstDays firstObject]] atIndex:0];
            self.horMonthFirstDays = [tmp copy];
            [self.horMonthTableView reloadData];
            [self.horMonthTableView displayRow:1 animation:NO];
            [self.horMonthTableView displayRow:0 animation:YES];
        }
    }else{
        NSInteger index = ((HorizontalTableViewCell *)(self.horWeekTableView.visibleCells[0])).row;
        if (index > 0) {
            [self.horWeekTableView displayRow:index - 1 animation:YES];
        }else{
            NSMutableArray *tmp = [NSMutableArray arrayWithArray:self.horWeekFirstDays];
            [tmp insertObject:[CalendarMgr firstDayInLastWeekForDate:[self.horWeekFirstDays firstObject]] atIndex:0];
            self.horWeekFirstDays = [tmp copy];
            [self.horWeekTableView reloadData];
            [self.horWeekTableView displayRow:1 animation:NO];
            [self.horWeekTableView displayRow:0 animation:YES];
        }
    }
}

-(void)monthTitleViewClickOnNext{
    if (self.horWeekTableView.hidden) {
        NSInteger index = ((HorizontalTableViewCell *)(self.horMonthTableView.visibleCells[0])).row;
        NSInteger max = self.horMonthFirstDays.count;
        if (index < max - 1) {
            [self.horMonthTableView displayRow:index + 1 animation:YES];
        }else{
            NSMutableArray *tmp = [NSMutableArray arrayWithArray:self.horMonthFirstDays];
            [tmp addObject:[CalendarMgr firstDayInNextMonthForDate:[self.horMonthFirstDays lastObject]]];
            self.horMonthFirstDays = [tmp copy];
            [self.horMonthTableView reloadData];
            [self.horMonthTableView displayRow:max - 1 animation:NO];
            [self.horMonthTableView displayRow:max animation:YES];
        }
    }else{
        NSInteger index = ((HorizontalTableViewCell *)(self.horWeekTableView.visibleCells[0])).row;
        NSInteger max = self.horWeekFirstDays.count;
        if (index < max - 1) {
            [self.horWeekTableView displayRow:index + 1 animation:YES];
        }else{
            NSMutableArray *tmp = [NSMutableArray arrayWithArray:self.horWeekFirstDays];
            [tmp addObject:[CalendarMgr firstDayInNextWeekForDate:[self.horWeekFirstDays lastObject]]];
            self.horWeekFirstDays = [tmp copy];
            [self.horWeekTableView reloadData];
            [self.horWeekTableView displayRow:max - 1 animation:NO];
            [self.horWeekTableView displayRow:max animation:YES];
        }
    }
}

#pragma mark - monthbottom delegate

-(void)monthBottomViewClickOnFlex{
    if (self.delegate) {
        [self.delegate monthDidClickOnFlexButton];
    }
}

#pragma mark -
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
    if ([change[NSKeyValueChangeNewKey] isEqual:change[NSKeyValueChangeOldKey]]) {
        return;
    }
    if ([keyPath isEqualToString:@"frame"]) {
        
        CGRect new = [change[NSKeyValueChangeNewKey] CGRectValue];
        CGRect old = [change[NSKeyValueChangeOldKey] CGRectValue];
        
        if (new.size.height == [MonthView heightMax] || new.size.height == [MonthView heightMin]) {
            self.isHeightChanging = NO;
            self.lastHeight = self.height;
            [self.monthBottomView updateFlexState:(new.size.height == [MonthView heightMax] ? FLEX_OPEN : FLEX_CLOSE)];
        }
        if (old.size.height == [MonthView heightMax] || old.size.height == [MonthView heightMin]) {
            self.isHeightChanging = YES;
        }
        
        [self updateFrame];
    }
}


@end
